package com.cloudcc.mobile.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.beau.BeauInfoEntity;
import com.litesuits.android.log.Log;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lin on 2017/5/17.
 */

public class BeauInfoAboutAdapter extends BaseAdapter {

    int drawableId = R.drawable.cloudtab17_1;//设置一个默认的图片id
    int id = 2;
    private List<BeauInfoEntity.ObjRelation> relationList;//相关列表信息
    private LayoutInflater mInflater;
    public BeauInfoAboutAdapter(Context mContext) {
//        this.relationList = relationList;
        relationList = new ArrayList<>();
        this.mInflater = LayoutInflater.from(mContext);
    }
//    public void convert(ViewHolder holder, Object o) {
//        BeauInfoEntity.ObjRelation objRelation = (BeauInfoEntity.ObjRelation) o;
//        holder.setText(R.id.beau_info_about_item_tv, objRelation.relationName +" ("+objRelation.relationSize+")");
//        holder.setSrc(R.id.beau_info_about_item_image , getDrawableId(objRelation.relationName));
//    }

    //暂时实现，具体需要接口提供id
//    private int getDrawableId(String names) {
////        if(names.indexOf("联系人") != -1){
////            drawableId = R.drawable.cloudtab_must_004_1;
////        }if(names.indexOf("业务机会") != -1){
////            drawableId = R.drawable.cloudtab_must_012_1;
////        }if(names.indexOf("潜在客户") != -1){
////            drawableId = R.drawable.cloudtab_must_005_1;
////        }if(names.indexOf("客户") != -1){
////            drawableId = R.drawable.cloudtab_must_001_1;
////        }if(names.indexOf("费用报销") != -1){
////            drawableId = R.drawable.cloudtab2_1;
////        }if(names.indexOf("合同") != -1){
////            drawableId = R.drawable.cloudtab_must_007_1;
////        }if(names.indexOf("需求") != -1){
////            drawableId = R.drawable.cloudtab2_1;
////        }if(names.indexOf("计划") != -1){
////            drawableId = R.drawable.cloudtab5_1;
////        }if(names.indexOf("销售") != -1){
////            drawableId = R.drawable.cloudtab8_1;
////        }if(names.indexOf("报表") != -1){
////            drawableId = R.drawable.cloudtab_report_1;
////        }else{
//        try {
//
//        } catch (Exception ex) {
//
//        }
////    }
//        return drawableId;
//    }
    @Override
    public int getCount() {
        return relationList.size();
    }

    @Override
    public Object getItem(int i) {
        return relationList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder=new ViewHolder();
            convertView = mInflater.inflate(R.layout.beau_info_about_item, null);
            holder.textView = (TextView) convertView.findViewById(R.id.beau_info_about_item_tv);
            holder.imageView = (ImageView) convertView.findViewById(R.id.beau_info_about_item_image);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        try {
            Class drawable = R.drawable.class;
            String bgid = "cloudtab" + (i+2) + "_1";
            Field field2 = drawable.getField(bgid);
            drawableId = field2.getInt(field2.getName());
            holder.imageView.setImageResource(drawableId);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        holder.textView.setText(relationList.get(i).relationName +" ("+relationList.get(i).relationSize+")");

        return convertView;
    }
    class ViewHolder{
        TextView textView;
        ImageView imageView;
    }
    public void addData(List<BeauInfoEntity.ObjRelation> data) {
        if (relationList == null) {
            relationList = new ArrayList<BeauInfoEntity.ObjRelation>();
        }
        relationList.addAll(data);
        notifyDataSetChanged();
        Log.d("istrue", "过来了");
    }

    /**
     * 清空数据
     */
    public void clear() {
        if (relationList != null) {
            relationList.clear();
        }
        notifyDataSetChanged();
    }
}
